package recursion;

import java.util.Arrays;

public class InsertionSearch {
    public static void sort(int[] arr) {
        recursionInsertionSearch(arr, 0);
    }

    private static void recursionInsertionSearch(int[] arr, int low) {
        if (low == arr.length) {
            return;
        }
        int temporary = arr[low];
        int pointer = low - 1;
        while (pointer >= 0 && arr[pointer] > temporary) { // 指针不能越界
            arr[pointer + 1] = arr[pointer];
            pointer--;
        }
        if (pointer + 1 != low) { // 如果是第一个元素或者待排序元素刚好小于已排序元素 则不用赋值
            arr[pointer + 1] = temporary;
        }

        recursionInsertionSearch(arr, low + 1);
    }

    public static void main(String[] args) {
        int[] arr = {4, 5, 32, 1, 23, 8, 9, 45, 0, 2, 6};
        sort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
